package com.zxy.ziems.server.sync.service;

import com.zxy.ziems.server.sync.entity.SyncMetaEntity;
import com.zxy.ziems.server.sync.enums.SyncMetaType;
import com.zxy.ziems.server.sync.mapper.SyncMetaMapper;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author chdzq
 * @create 2024/04/01
 */
@Service
public class SyncMetaServiceImpl extends ServiceImplX<SyncMetaMapper, SyncMetaEntity>
        implements SyncMetaService {

    @Override
    public LocalDateTime getSyncTime(String type) {
        SyncMetaEntity one = getOne(SyncMetaEntity::getType, type);
        if (Objects.isNull(one)) {
            return null;
        }
        return one.getTime();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSyncTime(LocalDateTime time, String type) {
        SyncMetaEntity one = new SyncMetaEntity();
        one.setTime(time);
        one.setType(type);
        saveOrUpdate(one, SyncMetaEntity::getType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSyncTime(LocalDateTime time, SyncMetaType type) {
        saveSyncTime(time, type.getCode());
    }
}
